package com.lylg.picture_share.controller;

import com.lylg.picture_share.entity.Collect;
import com.lylg.picture_share.entity.Image;
import com.lylg.picture_share.entity.User;
import com.lylg.picture_share.service.CollectService;
import com.lylg.picture_share.service.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class CollectController {


    @Autowired
    private CollectService collectService;
    @Autowired
    private ImageService imageService;



    @RequestMapping("/front/cancelCollect")
    @ResponseBody//如果接受的是ajax请求，加一下注解
    public String doCacel(Integer image_id, HttpSession session){

        Integer userId=0;
        if(session.getAttribute("loginuser")!=null){
            userId=((User)session.getAttribute("loginuser")).getUser_id();
            Collect collect=new Collect(userId,image_id);

            collectService.cancelCollect(collect);
            return "取消成功";

        }
        else{
            return "请进行登录";
        }
    }


    @RequestMapping("/front/doCollect")
    @ResponseBody//如果接受的是ajax请求，加一下注解
    public String doCollect(Integer image_id, HttpSession session){

        Integer userId=0;
        if(session.getAttribute("loginuser")!=null){
            userId=((User)session.getAttribute("loginuser")).getUser_id();
            Collect collect=new Collect(userId,image_id);

            collectService.doCollect(collect);
            return "收藏成功";

        }
        else{
            return "请进行登录";
        }
    }
    @RequestMapping("/front/myCollectUI")
    public String goMyCollectUI(Map<String,Object> model, HttpSession session){

        Integer userId=0;
        if(session.getAttribute("loginuser")!=null) {
            userId = ((User) session.getAttribute("loginuser")).getUser_id();
        }
//        List<Video> videos=videoService.getMyColelctVideos(userId);
        List<Image> images = imageService.getMyColelctImages(userId);
        System.out.println(images);
        model.put("images",images);
        return "/front/myCollect.html";
    }
}

